package com.operate.api.operate.user.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.operate.api.base.Constants;
import com.operate.api.base.Type;
import com.operate.api.operate.user.dao.OperateUserDao;
import com.operate.api.operate.user.domain.po.OperateUserPO;
import com.operate.api.operate.user.domain.vo.*;
import com.operate.api.operate.user.service.IOperateUserService;
import com.operate.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
@Transactional
@Slf4j
public class OperateUserServiceImpl implements IOperateUserService {

    @Autowired
    private OperateUserDao dao;


    @Override
    public UserLoginResVO login(UserLoginReqVO userLogin) {
        Example sysExample = new Example(OperateUserPO.class);
        sysExample.and().andEqualTo("userAccount",userLogin.getLoginName())
                .andEqualTo("delFlag", false);
        OperateUserPO loginUser=dao.selectOneByExample(sysExample);
        if(loginUser==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),"登陆用户信息不存在！");
        }
        if(StringUtils.isBlank(loginUser.getUserPassword())||!loginUser.getUserPassword().equals(userLogin.getPassword())){
            throw new BusinessException(Type.FAIL.getCode(),"账号密码错误！");
        }
        if(loginUser.getUseState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"账号已被禁用！");
        }
        StpUtil.login(loginUser.getId());
        UserLoginResVO userLoginResVO =new UserLoginResVO();
        userLoginResVO.setId(loginUser.getId());
        userLoginResVO.setToken(StpUtil.getTokenValue());
        userLoginResVO.setUserName(loginUser.getUserName());
        return userLoginResVO;
    }

    @Override
    public UserVO getOne() {
        OperateUserPO userPO=dao.selectByPrimaryKey(StpUtil.getLoginId());
        if(userPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        UserVO userVO=new UserVO();
        BeanUtils.copyProperties(userPO, userVO);
        return userVO;
    }

    @Override
    public void loginOut() {
        StpUtil.logout();
    }


    @Override
    public void updatePassword(UserPasswordUpdateVO passwordUpdateVO) {
        OperateUserPO userPO = dao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
        if (userPO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "用户信息不存在");
        }
        if(!userPO.getUserPassword().equals(passwordUpdateVO.getOlePassword())){
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "密码修改失败，旧密码不正确");
        }
        userPO.setUserPassword(passwordUpdateVO.getNewPassword());
        userPO.preUpdate();
        int i = dao.updateByPrimaryKeySelective(userPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }

    }

    @Override
    public List<UserVO> getList(UserSearchVO searchVO) {
        return dao.getList(searchVO);
    }

    @Override
    public void add(UserAddVO vo) {
        OperateUserPO po = new OperateUserPO();
        BeanUtils.copyProperties(vo, po);
        Example example = new Example(OperateUserPO.class);
        example.and().andEqualTo("userAccount", po.getUserAccount()).andEqualTo("delFlag", false);
        List<OperateUserPO> list = dao.selectByExample(example);
        if (!list.isEmpty()) {
            throw new BusinessException(Type.EXIST_ERROR.getCode(),  "用户名已存在");
        }
        po.preInsert();
        po.setUseState(1);
        int i = dao.insertSelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        OperateUserPO userPO = dao.selectByPrimaryKey(id);
        if (userPO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "用户不存在");
        }
        OperateUserPO po = new OperateUserPO();
        if(userPO.getId().equals(StpUtil.getLoginId())) {
            throw new BusinessException(Type.FAIL.getCode(),"删除失败,当前用户无法删除!");
        }
        if(userPO.getUserAccount().equals("admin")) {
            throw new BusinessException(Type.FAIL.getCode(),"超级管理员不能删除");
        }
        po.setId(id);
        po.preDelete();
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void disableUser(UserStateVO vo) {
        if (StringUtils.isBlank(vo.getId())) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        OperateUserPO userPO = dao.selectByPrimaryKey(vo.getId());
        if (userPO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "用户不存在");
        }
        userPO.setUseState(vo.getUseState());
        userPO.preUpdate();
        int i = dao.updateByPrimaryKeySelective(userPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void update(UserUpdateVO vo) {
        OperateUserPO po = new OperateUserPO();
        BeanUtils.copyProperties(vo, po);
        OperateUserPO userPO = dao.selectByPrimaryKey(po.getId());
        if (userPO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "用户不存在");
        }
        po.preUpdate();
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void resetPassword(UserPasswordVO updateVO) {
        if (StringUtils.isBlank(updateVO.getId())) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        OperateUserPO userPO = dao.selectByPrimaryKey(updateVO.getId());
        if (userPO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "用户不存在");
        }
        userPO.setUserPassword(updateVO.getResetPassword());
        userPO.preUpdate();
        int i = dao.updateByPrimaryKeySelective(userPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

}